const {
  menuMapClient
} = require('../../../config/menu')
const { queryApprovalOnOff } = require('../../../api/company')
const {
  getUserType,
  getUserBaseInformation,
  setApprovalCustomerWorkOrder,
  getApproveCustomerWordOrder
} = require('../../../utils/strorage')
const { routeWithToken, getEventData, to } = getApp();
function getMenu(approvalCustomerWorkOrder) {
  const userType = getUserType()
  return menuMapClient.filter(item => {
    if (!item.limit) return true
    return item.limit[0] === userType && item.limit[1] === approvalCustomerWorkOrder
  })
}
Component({
  properties: {

  },

  lifetimes: {
    attached() {
      const approveCustomerWorkOrder = getApproveCustomerWordOrder()
      this.setData({
        indexMenuList: getMenu(approveCustomerWorkOrder)
      })
    }
  },

  pageLifetimes: {
    show() {
      this.getQueryApprovalOnOff()
    }
  },
  data: {
    indexMenuList: getMenu(),
    userInfo: {}
  },
  methods: {
    async getQueryApprovalOnOff() {
      const [err, data] = await to(queryApprovalOnOff());

      if (!err) {
        const userInfo = getUserBaseInformation()
        this.setData({
          userInfo,
          indexMenuList: getMenu(data.approvalCustomerWorkOrder),
          approvalCustomerWorkOrder: data.approvalCustomerWorkOrder
        })

        setApprovalCustomerWorkOrder(data.approvalCustomerWorkOrder)
      }
    },
    onClickUserName() {
      routeWithToken('/tabbar/mine/mine')
    },

    handleMenuIconClick(event) {
      const { data } = getEventData(event)
      this.triggerEvent('clickIcon', data)
    }
  }
})